package com.jiuzhe.commercial.ui.house.editor;

import android.app.Dialog;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.jiuzhe.commercial.R;
import com.jiuzhe.commercial.base.BaseActivity;
import com.jiuzhe.commercial.bean.HouseStayBean;
import com.jiuzhe.commercial.bean.HouseStayListBean;
import com.jiuzhe.commercial.constant.Constants;
import com.jiuzhe.commercial.http.CallBack;
import com.jiuzhe.commercial.http.HttpClient;
import com.jiuzhe.commercial.utils.RxBus;
import com.jiuzhe.commercial.utils.SPUtil;
import com.jiuzhe.commercial.utils.UIUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by huanghao on 2018/6/7.
 */

public class EditorHomePriceActivity extends BaseActivity{
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.tv_roomPrice)
    TextView tvRoomPrice;

    private String mDepositPrice;
    private String mDayPrice;
    private HouseStayBean houseStayBean;
    private String price;
    private String roomBond;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_editor_homestay_prices);
        setTitle("房源价格");
        setRringTitle("保存");
        setOnClik();
    }

    private void setOnClik() {
        tv_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nextStep();
            }
        });
    }


    @Override
    protected void loadData() {
        houseStayBean = new HouseStayBean();
        if (null != Constants.HOUSESTAYLISTBEAN) {
            price = Constants.HOUSESTAYLISTBEAN.getDefPrice();
            roomBond = Constants.HOUSESTAYLISTBEAN.getRoomBond();
            tvRoomPrice.setText("日价 " + price + "元   " + "压金 " + roomBond + "元");
            etName.setText(Constants.HOUSESTAYLISTBEAN.getSkuName());
            etName.setSelection(Constants.HOUSESTAYLISTBEAN.getSkuName().length());//将光标移至文字末尾
        }
    }

    @OnClick({R.id.tv_roomPrice})
    public void onclick(View view) {
        switch (view.getId()) {
            case R.id.tv_roomPrice:
                setPrice();
                break;
        }
    }

    private void setPrice() {
        final Dialog alertDialog = new Dialog(this, R.style.EasyListDialogStyle);
        mView = LayoutInflater.from(this).inflate(R.layout.dialog_house_price, null);
        alertDialog.setContentView(mView);
        alertDialog.show();
        Window window = alertDialog.getWindow();
        window.setGravity(Gravity.CENTER);
        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = this.getResources().getDisplayMetrics().widthPixels/5*4;
        window.setAttributes(lp);
        final EditText etDayPrice = alertDialog.findViewById(R.id.et_day_price);
        final EditText etDeposit = alertDialog.findViewById(R.id.et_deposit);
        Button btnCancel = alertDialog.findViewById(R.id.btn_cancel);
        Button btnOk = alertDialog.findViewById(R.id.bt_ok);
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.btn_cancel:
                        alertDialog.dismiss();
                        break;
                    case R.id.bt_ok:
                        mDayPrice = etDayPrice.getText().toString().trim();
                        mDepositPrice = etDeposit.getText().toString().trim();
                        houseStayBean.setRoomPrice(mDayPrice);
                       houseStayBean.setRoomBond(mDepositPrice);
                        if (!TextUtils.isEmpty(mDayPrice) && !TextUtils.isEmpty(mDepositPrice)) {
                            tvRoomPrice.setText("日价 " + mDayPrice + "元" + "   " + "压金 " + mDepositPrice + "元");
                            alertDialog.dismiss();
                        } else {
                            UIUtils.showToastSafe("日价和压金价格不能为空");
                        }
                        break;
                }
            }
        };
        alertDialog.findViewById(R.id.bt_ok).setOnClickListener(listener);
        alertDialog.findViewById(R.id.btn_cancel).setOnClickListener(listener);
    }


    private void nextStep() {
        final HouseStayListBean houseStayListBean = new HouseStayListBean();
        String eName =  etName.getText().toString().trim();
        String eRoomPrice =  tvRoomPrice.getText().toString().trim();

        if (TextUtils.isEmpty(eName)) {
            UIUtils.showToastSafe("请输入房源名称！");
            return;
        }
        if (TextUtils.isEmpty(eRoomPrice)) {
            UIUtils.showToastSafe("请输入房源价格和押金！");
            return;
        }

        HashMap<String, String> map = new HashMap<>();
        String token = SPUtil.getString(EditorHomePriceActivity.this, Constants.TOKEN);
        String id = SPUtil.getString(EditorHomePriceActivity.this, Constants.ID);
        String autoSignStr = Constants.SKUUPDATING.substring(Constants.SKUUPDATING.lastIndexOf("0/") + 1) + "?token=";
        String signStr = UIUtils.Md5(autoSignStr + token);
        map.put("token", id);
        map.put("sign", signStr);
        map.put("skuId", Constants.HOUSESTAYLISTBEAN.getId());

        map.put("skuName", eName);

        String tvPrice = "日价 " + price + "元" + "   " + "压金 " + roomBond + "元";

         if (eRoomPrice.equals(tvPrice)){
             map.put("roomPrice", price);
             map.put("roomBond", roomBond);
         }else {
             map.put("roomPrice", houseStayBean.getRoomPrice());
             map.put("roomBond", houseStayBean.getRoomBond());
         }


        HttpClient.post(this, Constants.SKUUPDATING, map, new CallBack<String>() {
            @Override
            public void onSuccess(String result) {
                UIUtils.showToastSafe("修改成功");
                RxBus.getInstance().post(houseStayListBean);
                finish();
            }
        });
    }
}
